﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginFramework.Common;
using QLBH.Base;
using System.Windows.Forms;
using System.Drawing;
using QLBH.Model.Controller;
using QLBH.DataModel;
using QLBH.FeatureBase;
using QLBH.ModelInterface;
using QLBH.ModelFactory;
using QLBH.StandardUI;
using QLBH.Category;

namespace QLBH.SaleProduct
{    
    public class BanHangView:FormViewBase
    {
        #region properties
        private TextBox tbxTongTienTT;
        private StandardUI.DataGridViewEx dataGridViewEx1;
        private ComboBox cmbKhoHang;
        private TextBox tbxChietKhau;
        private Label label17;
        private Label lbNgayTao;
        private Label lbNguoiTao;
        private Label label13;
        private Label label11;
        private Label label9;
        private TextBox tbxThue;
        private Label lbsophieu;
        private Label label8;
        private Label label15;
        private TextBox tbxSoLuong;
        private Button btnDelete;
        private GroupBox groupBox2;
        private CheckBox chbDungMaVach;
        private Button btnAdd;
        private StandardUI.AutoCompleteTextBox tbxMatHangSeach;
        private Label label18;
        private Label label19;
        private Label label16;
        private DevComponents.DotNetBar.Controls.TextBoxX tbxKhachHangTra;
        private TextBox tbxTienThua;
        private Panel panel1;
        private TextBox tbxTongTien;
        private Label label7;
        private Label label6;
        private Label label5;
        private GroupBox groupBox1;
        private TableLayoutPanel tableLayoutPanel1;
        private ToolStrip toolStrip1;
        private ToolStripButton btnSaveAndNew;
        private ToolStripButton btnPrintPreview;
        private ToolStripButton btnPrint;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripButton btnSearch;
        private Label label2;
        private Label label4;        
        private Label label3;        
        private RadioButton rdbBanBuon;
        private RadioButton rdbBanLe;
        private Label lbKhachHang;
        private LinkLabel linklbKhachHang;
        private DataGridViewTextBoxColumn clHDBHID;
        private DataGridViewTextBoxColumn clMHID;
        private DataGridViewTextBoxColumn Column1;
        private DataGridViewTextBoxColumn clMaMH;
        private DataGridViewTextBoxColumn clTenMH;
        private DataGridViewTextBoxColumn clDVT;
        private DataGridViewTextBoxColumn clsoluong;
        private DataGridViewTextBoxColumn clGiaBan;
        private DataGridViewTextBoxColumn clchietkhau;
        private DataGridViewTextBoxColumn clThanhTien;
        private DataGridViewTextBoxColumn clGhiChu;
        private TextBox tbxDonGia;
        private Label label1;
        private RichTextBox rtbGhiChu;
        #endregion    
        
        
        public BanHangView()
            :base()
        {
            InitializeComponent();           
        }

        private IHDBanHang model;

        public IHDBanHang Model
        {
            get { return model; }
            set { model = value; }
        }

        private ListView listViewPopup;

        private MATHANG selected;

        private List<MATHANG> mathangs;

        private eDlgType functionType = eDlgType.ADD;
        private eDlgType dlgType = eDlgType.ADD;

        private IHD_MatHangModel editItem;

        private decimal totalMoney = 0;

        private double thue = 10;

        private decimal chietkhau = 0;

        private decimal khachangtra = 0;
        private decimal tienthua = 0;

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BanHangView));
            this.tbxTongTienTT = new System.Windows.Forms.TextBox();
            this.dataGridViewEx1 = new QLBH.StandardUI.DataGridViewEx();
            this.clHDBHID = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.clMHID = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.clMaMH = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.clTenMH = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.clDVT = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.clsoluong = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.clGiaBan = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.clchietkhau = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.clThanhTien = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.clGhiChu = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.cmbKhoHang = new System.Windows.Forms.ComboBox();
            this.tbxChietKhau = new System.Windows.Forms.TextBox();
            this.label17 = new System.Windows.Forms.Label();
            this.lbNgayTao = new System.Windows.Forms.Label();
            this.lbNguoiTao = new System.Windows.Forms.Label();
            this.label13 = new System.Windows.Forms.Label();
            this.label11 = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.tbxThue = new System.Windows.Forms.TextBox();
            this.lbsophieu = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.label15 = new System.Windows.Forms.Label();
            this.tbxSoLuong = new System.Windows.Forms.TextBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.tbxDonGia = new System.Windows.Forms.TextBox();
            this.chbDungMaVach = new System.Windows.Forms.CheckBox();
            this.btnDelete = new System.Windows.Forms.Button();
            this.btnAdd = new System.Windows.Forms.Button();
            this.tbxMatHangSeach = new QLBH.StandardUI.AutoCompleteTextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label18 = new System.Windows.Forms.Label();
            this.label19 = new System.Windows.Forms.Label();
            this.label16 = new System.Windows.Forms.Label();
            this.tbxKhachHangTra = new DevComponents.DotNetBar.Controls.TextBoxX();
            this.tbxTienThua = new System.Windows.Forms.TextBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.tbxTongTien = new System.Windows.Forms.TextBox();
            this.label7 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.linklbKhachHang = new System.Windows.Forms.LinkLabel();
            this.rdbBanBuon = new System.Windows.Forms.RadioButton();
            this.rdbBanLe = new System.Windows.Forms.RadioButton();
            this.lbKhachHang = new System.Windows.Forms.Label();
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.btnSaveAndNew = new System.Windows.Forms.ToolStripButton();
            this.btnPrintPreview = new System.Windows.Forms.ToolStripButton();
            this.btnPrint = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.btnSearch = new System.Windows.Forms.ToolStripButton();
            this.label2 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.rtbGhiChu = new System.Windows.Forms.RichTextBox();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridViewEx1)).BeginInit();
            this.groupBox2.SuspendLayout();
            this.panel1.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.tableLayoutPanel1.SuspendLayout();
            this.toolStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // tbxTongTienTT
            // 
            this.tbxTongTienTT.Enabled = false;
            this.tbxTongTienTT.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxTongTienTT.ForeColor = System.Drawing.Color.Red;
            this.tbxTongTienTT.Location = new System.Drawing.Point(474, 23);
            this.tbxTongTienTT.Name = "tbxTongTienTT";
            this.tbxTongTienTT.Size = new System.Drawing.Size(178, 29);
            this.tbxTongTienTT.TabIndex = 1;
            this.tbxTongTienTT.Text = "0";
            this.tbxTongTienTT.TextChanged += new System.EventHandler(this.tbxTongTienTT_TextChanged);
            // 
            // dataGridViewEx1
            // 
            this.dataGridViewEx1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGridViewEx1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
            this.dataGridViewEx1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
            this.dataGridViewEx1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridViewEx1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.clHDBHID,
            this.clMHID,
            this.Column1,
            this.clMaMH,
            this.clTenMH,
            this.clDVT,
            this.clsoluong,
            this.clGiaBan,
            this.clchietkhau,
            this.clThanhTien,
            this.clGhiChu});
            this.dataGridViewEx1.Location = new System.Drawing.Point(6, 73);
            this.dataGridViewEx1.Name = "dataGridViewEx1";
            this.dataGridViewEx1.ReadOnly = true;
            this.dataGridViewEx1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dataGridViewEx1.Size = new System.Drawing.Size(0, 0);
            this.dataGridViewEx1.TabIndex = 17;
            this.dataGridViewEx1.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewEx1_CellMouseDoubleClick);
            this.dataGridViewEx1.SelectionChanged += new System.EventHandler(this.dataGridViewEx1_SelectionChanged);
            // 
            // clHDBHID
            // 
            this.clHDBHID.DataPropertyName = "HDBHID";
            this.clHDBHID.HeaderText = "Column1";
            this.clHDBHID.Name = "clHDBHID";
            this.clHDBHID.ReadOnly = true;
            this.clHDBHID.Visible = false;
            this.clHDBHID.Width = 73;
            // 
            // clMHID
            // 
            this.clMHID.DataPropertyName = "MHID";
            this.clMHID.HeaderText = "Column1";
            this.clMHID.Name = "clMHID";
            this.clMHID.ReadOnly = true;
            this.clMHID.Visible = false;
            this.clMHID.Width = 73;
            // 
            // Column1
            // 
            this.Column1.DataPropertyName = "KHOID";
            this.Column1.HeaderText = "Column1";
            this.Column1.Name = "Column1";
            this.Column1.ReadOnly = true;
            this.Column1.Visible = false;
            this.Column1.Width = 73;
            // 
            // clMaMH
            // 
            this.clMaMH.DataPropertyName = "MaMH";
            this.clMaMH.HeaderText = "Mã MH";
            this.clMaMH.Name = "clMaMH";
            this.clMaMH.ReadOnly = true;
            this.clMaMH.Width = 76;
            // 
            // clTenMH
            // 
            this.clTenMH.DataPropertyName = "TenMH";
            this.clTenMH.HeaderText = "Tên MH";
            this.clTenMH.Name = "clTenMH";
            this.clTenMH.ReadOnly = true;
            this.clTenMH.Width = 81;
            // 
            // clDVT
            // 
            this.clDVT.DataPropertyName = "DVT";
            this.clDVT.HeaderText = "DVT";
            this.clDVT.Name = "clDVT";
            this.clDVT.ReadOnly = true;
            this.clDVT.Width = 61;
            // 
            // clsoluong
            // 
            this.clsoluong.DataPropertyName = "SOLUONG";
            this.clsoluong.HeaderText = "Số Lượng";
            this.clsoluong.Name = "clsoluong";
            this.clsoluong.ReadOnly = true;
            this.clsoluong.Width = 90;
            // 
            // clGiaBan
            // 
            this.clGiaBan.DataPropertyName = "GIABAN";
            this.clGiaBan.HeaderText = "Giá Bán";
            this.clGiaBan.Name = "clGiaBan";
            this.clGiaBan.ReadOnly = true;
            this.clGiaBan.Width = 81;
            // 
            // clchietkhau
            // 
            this.clchietkhau.DataPropertyName = "CHIETKHAU";
            this.clchietkhau.HeaderText = "Chiết Khấu";
            this.clchietkhau.Name = "clchietkhau";
            this.clchietkhau.ReadOnly = true;
            this.clchietkhau.Width = 96;
            // 
            // clThanhTien
            // 
            this.clThanhTien.DataPropertyName = "THANHTIEN";
            this.clThanhTien.HeaderText = "Thành Tiền";
            this.clThanhTien.Name = "clThanhTien";
            this.clThanhTien.ReadOnly = true;
            this.clThanhTien.Width = 101;
            // 
            // clGhiChu
            // 
            this.clGhiChu.DataPropertyName = "GHICHU";
            this.clGhiChu.HeaderText = "Ghi Chú";
            this.clGhiChu.Name = "clGhiChu";
            this.clGhiChu.ReadOnly = true;
            this.clGhiChu.Visible = false;
            this.clGhiChu.Width = 70;
            // 
            // cmbKhoHang
            // 
            this.cmbKhoHang.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbKhoHang.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.cmbKhoHang.FormattingEnabled = true;
            this.cmbKhoHang.Location = new System.Drawing.Point(112, 60);
            this.cmbKhoHang.Name = "cmbKhoHang";
            this.cmbKhoHang.Size = new System.Drawing.Size(240, 24);
            this.cmbKhoHang.TabIndex = 1;
            this.cmbKhoHang.SelectedIndexChanged += new System.EventHandler(this.cmbKhoHang_SelectedIndexChanged);
            // 
            // tbxChietKhau
            // 
            this.tbxChietKhau.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxChietKhau.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.tbxChietKhau.Location = new System.Drawing.Point(312, 23);
            this.tbxChietKhau.Name = "tbxChietKhau";
            this.tbxChietKhau.Size = new System.Drawing.Size(143, 29);
            this.tbxChietKhau.TabIndex = 1;
            this.tbxChietKhau.Text = "0";
            this.tbxChietKhau.TextChanged += new System.EventHandler(this.tbxChietKhau_TextChanged);
            // 
            // label17
            // 
            this.label17.AutoSize = true;
            this.label17.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label17.ForeColor = System.Drawing.Color.Red;
            this.label17.Location = new System.Drawing.Point(470, 4);
            this.label17.Name = "label17";
            this.label17.Size = new System.Drawing.Size(181, 16);
            this.label17.TabIndex = 0;
            this.label17.Text = "Tổng tiền cần thành toán:";
            // 
            // lbNgayTao
            // 
            this.lbNgayTao.AutoSize = true;
            this.lbNgayTao.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbNgayTao.Location = new System.Drawing.Point(437, 63);
            this.lbNgayTao.Name = "lbNgayTao";
            this.lbNgayTao.Size = new System.Drawing.Size(79, 16);
            this.lbNgayTao.TabIndex = 0;
            this.lbNgayTao.Text = "Người tạo:";
            // 
            // lbNguoiTao
            // 
            this.lbNguoiTao.AutoSize = true;
            this.lbNguoiTao.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbNguoiTao.Location = new System.Drawing.Point(437, 29);
            this.lbNguoiTao.Name = "lbNguoiTao";
            this.lbNguoiTao.Size = new System.Drawing.Size(79, 16);
            this.lbNguoiTao.TabIndex = 0;
            this.lbNguoiTao.Text = "Người tạo:";
            // 
            // label13
            // 
            this.label13.AutoSize = true;
            this.label13.Location = new System.Drawing.Point(362, 63);
            this.label13.Name = "label13";
            this.label13.Size = new System.Drawing.Size(66, 16);
            this.label13.TabIndex = 0;
            this.label13.Text = "Ngày tạo:";
            // 
            // label11
            // 
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(362, 29);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(69, 16);
            this.label11.TabIndex = 0;
            this.label11.Text = "Người tạo:";
            // 
            // label9
            // 
            this.label9.AutoSize = true;
            this.label9.Location = new System.Drawing.Point(18, 63);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(93, 16);
            this.label9.TabIndex = 0;
            this.label9.Text = "Kho hàng (F7):";
            // 
            // tbxThue
            // 
            this.tbxThue.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxThue.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.tbxThue.Location = new System.Drawing.Point(197, 23);
            this.tbxThue.Name = "tbxThue";
            this.tbxThue.Size = new System.Drawing.Size(97, 29);
            this.tbxThue.TabIndex = 1;
            this.tbxThue.Text = "10";
            this.tbxThue.TextChanged += new System.EventHandler(this.tbxThue_TextChanged);
            // 
            // lbsophieu
            // 
            this.lbsophieu.AutoSize = true;
            this.lbsophieu.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbsophieu.Location = new System.Drawing.Point(109, 29);
            this.lbsophieu.Name = "lbsophieu";
            this.lbsophieu.Size = new System.Drawing.Size(73, 16);
            this.lbsophieu.TabIndex = 0;
            this.lbsophieu.Text = "Số phiếu:";
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(18, 29);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(64, 16);
            this.label8.TabIndex = 0;
            this.label8.Text = "Số phiếu:";
            // 
            // label15
            // 
            this.label15.AutoSize = true;
            this.label15.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label15.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.label15.Location = new System.Drawing.Point(192, 4);
            this.label15.Name = "label15";
            this.label15.Size = new System.Drawing.Size(105, 16);
            this.label15.TabIndex = 0;
            this.label15.Text = "Thuế (%) (F5):";
            // 
            // tbxSoLuong
            // 
            this.tbxSoLuong.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxSoLuong.ForeColor = System.Drawing.Color.Gray;
            this.tbxSoLuong.Location = new System.Drawing.Point(526, 39);
            this.tbxSoLuong.Name = "tbxSoLuong";
            this.tbxSoLuong.Size = new System.Drawing.Size(68, 29);
            this.tbxSoLuong.TabIndex = 14;
            this.tbxSoLuong.Text = "0";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.tbxDonGia);
            this.groupBox2.Controls.Add(this.chbDungMaVach);
            this.groupBox2.Controls.Add(this.dataGridViewEx1);
            this.groupBox2.Controls.Add(this.tbxSoLuong);
            this.groupBox2.Controls.Add(this.btnDelete);
            this.groupBox2.Controls.Add(this.btnAdd);
            this.groupBox2.Controls.Add(this.tbxMatHangSeach);
            this.groupBox2.Controls.Add(this.label1);
            this.groupBox2.Controls.Add(this.label18);
            this.groupBox2.Controls.Add(this.label19);
            this.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.groupBox2.Location = new System.Drawing.Point(3, 129);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(1, 1);
            this.groupBox2.TabIndex = 7;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Danh sách mặt hàng";
            // 
            // tbxDonGia
            // 
            this.tbxDonGia.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxDonGia.ForeColor = System.Drawing.Color.Gray;
            this.tbxDonGia.Location = new System.Drawing.Point(620, 39);
            this.tbxDonGia.Name = "tbxDonGia";
            this.tbxDonGia.Size = new System.Drawing.Size(171, 29);
            this.tbxDonGia.TabIndex = 19;
            this.tbxDonGia.Text = "0";
            this.tbxDonGia.MouseClick += new System.Windows.Forms.MouseEventHandler(this.tbxDonGia_MouseClick);
            // 
            // chbDungMaVach
            // 
            this.chbDungMaVach.AutoSize = true;
            this.chbDungMaVach.Location = new System.Drawing.Point(138, 17);
            this.chbDungMaVach.Name = "chbDungMaVach";
            this.chbDungMaVach.Size = new System.Drawing.Size(113, 20);
            this.chbDungMaVach.TabIndex = 18;
            this.chbDungMaVach.Text = "Dùng mã vạch";
            this.chbDungMaVach.UseVisualStyleBackColor = true;
            // 
            // btnDelete
            // 
            this.btnDelete.Enabled = false;
            this.btnDelete.Image = ((System.Drawing.Image)(resources.GetObject("btnDelete.Image")));
            this.btnDelete.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnDelete.Location = new System.Drawing.Point(919, 37);
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new System.Drawing.Size(99, 29);
            this.btnDelete.TabIndex = 16;
            this.btnDelete.Text = "Xóa (Del)";
            this.btnDelete.UseVisualStyleBackColor = true;
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            // 
            // btnAdd
            // 
            this.btnAdd.Image = ((System.Drawing.Image)(resources.GetObject("btnAdd.Image")));
            this.btnAdd.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnAdd.Location = new System.Drawing.Point(797, 38);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(120, 29);
            this.btnAdd.TabIndex = 15;
            this.btnAdd.Text = "Thêm (Enter)";
            this.btnAdd.UseVisualStyleBackColor = true;
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // tbxMatHangSeach
            // 
            this.tbxMatHangSeach.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxMatHangSeach.ForeColor = System.Drawing.Color.Gray;
            this.tbxMatHangSeach.Location = new System.Drawing.Point(9, 38);
            this.tbxMatHangSeach.Name = "tbxMatHangSeach";
            this.tbxMatHangSeach.PopupControl = null;
            this.tbxMatHangSeach.Size = new System.Drawing.Size(507, 29);
            this.tbxMatHangSeach.TabIndex = 13;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(523, 17);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(87, 16);
            this.label1.TabIndex = 11;
            this.label1.Text = "Số lượng (F2)";
            // 
            // label18
            // 
            this.label18.AutoSize = true;
            this.label18.Location = new System.Drawing.Point(617, 19);
            this.label18.Name = "label18";
            this.label18.Size = new System.Drawing.Size(80, 16);
            this.label18.TabIndex = 11;
            this.label18.Text = "Đơn giá (F3)";
            // 
            // label19
            // 
            this.label19.AutoSize = true;
            this.label19.Location = new System.Drawing.Point(6, 19);
            this.label19.Name = "label19";
            this.label19.Size = new System.Drawing.Size(92, 16);
            this.label19.TabIndex = 12;
            this.label19.Text = "Mặt hàng (F1):";
            // 
            // label16
            // 
            this.label16.AutoSize = true;
            this.label16.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label16.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.label16.Location = new System.Drawing.Point(314, 4);
            this.label16.Name = "label16";
            this.label16.Size = new System.Drawing.Size(115, 16);
            this.label16.TabIndex = 0;
            this.label16.Text = "Chiết khấu (F6):";
            // 
            // tbxKhachHangTra
            // 
            // 
            // 
            // 
            this.tbxKhachHangTra.Border.Class = "TextBoxBorder";
            this.tbxKhachHangTra.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxKhachHangTra.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.tbxKhachHangTra.Location = new System.Drawing.Point(671, 23);
            this.tbxKhachHangTra.Name = "tbxKhachHangTra";
            this.tbxKhachHangTra.Size = new System.Drawing.Size(181, 29);
            this.tbxKhachHangTra.TabIndex = 2;
            this.tbxKhachHangTra.Text = "0";
            this.tbxKhachHangTra.TextChanged += new System.EventHandler(this.tbxKhachHangTra_TextChanged);
            // 
            // tbxTienThua
            // 
            this.tbxTienThua.Enabled = false;
            this.tbxTienThua.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxTienThua.ForeColor = System.Drawing.Color.Red;
            this.tbxTienThua.Location = new System.Drawing.Point(872, 23);
            this.tbxTienThua.Name = "tbxTienThua";
            this.tbxTienThua.Size = new System.Drawing.Size(176, 29);
            this.tbxTienThua.TabIndex = 1;
            this.tbxTienThua.Text = "0";
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.tbxKhachHangTra);
            this.panel1.Controls.Add(this.tbxTienThua);
            this.panel1.Controls.Add(this.tbxTongTienTT);
            this.panel1.Controls.Add(this.tbxChietKhau);
            this.panel1.Controls.Add(this.tbxThue);
            this.panel1.Controls.Add(this.tbxTongTien);
            this.panel1.Controls.Add(this.label7);
            this.panel1.Controls.Add(this.label6);
            this.panel1.Controls.Add(this.label17);
            this.panel1.Controls.Add(this.label16);
            this.panel1.Controls.Add(this.label15);
            this.panel1.Controls.Add(this.label5);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, -61);
            this.panel1.Margin = new System.Windows.Forms.Padding(0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(1, 62);
            this.panel1.TabIndex = 5;
            // 
            // tbxTongTien
            // 
            this.tbxTongTien.Enabled = false;
            this.tbxTongTien.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxTongTien.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.tbxTongTien.Location = new System.Drawing.Point(6, 23);
            this.tbxTongTien.Name = "tbxTongTien";
            this.tbxTongTien.Size = new System.Drawing.Size(176, 29);
            this.tbxTongTien.TabIndex = 1;
            this.tbxTongTien.Text = "0";
            this.tbxTongTien.TextChanged += new System.EventHandler(this.tbxTongTien_TextChanged);
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label7.ForeColor = System.Drawing.Color.Red;
            this.label7.Location = new System.Drawing.Point(867, 3);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(76, 16);
            this.label7.TabIndex = 0;
            this.label7.Text = "Tiền thừa:";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label6.ForeColor = System.Drawing.Color.Red;
            this.label6.Location = new System.Drawing.Point(667, 4);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(145, 16);
            this.label6.TabIndex = 0;
            this.label6.Text = "Khách hàng trả (F4):";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label5.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.label5.Location = new System.Drawing.Point(3, 4);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(137, 16);
            this.label5.TabIndex = 0;
            this.label5.Text = "Tổng tiền hóa đơn:";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.linklbKhachHang);
            this.groupBox1.Controls.Add(this.rdbBanBuon);
            this.groupBox1.Controls.Add(this.rdbBanLe);
            this.groupBox1.Controls.Add(this.cmbKhoHang);
            this.groupBox1.Controls.Add(this.lbKhachHang);
            this.groupBox1.Controls.Add(this.lbNgayTao);
            this.groupBox1.Controls.Add(this.lbNguoiTao);
            this.groupBox1.Controls.Add(this.label13);
            this.groupBox1.Controls.Add(this.label11);
            this.groupBox1.Controls.Add(this.label9);
            this.groupBox1.Controls.Add(this.lbsophieu);
            this.groupBox1.Controls.Add(this.label8);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.groupBox1.Location = new System.Drawing.Point(3, 31);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(1, 92);
            this.groupBox1.TabIndex = 4;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Thông tin phiếu xuất";
            // 
            // linklbKhachHang
            // 
            this.linklbKhachHang.AutoSize = true;
            this.linklbKhachHang.Location = new System.Drawing.Point(739, 68);
            this.linklbKhachHang.Name = "linklbKhachHang";
            this.linklbKhachHang.Size = new System.Drawing.Size(81, 16);
            this.linklbKhachHang.TabIndex = 3;
            this.linklbKhachHang.TabStop = true;
            this.linklbKhachHang.Text = "Khách hàng:";
            this.linklbKhachHang.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linklbKhachHang_LinkClicked);
            // 
            // rdbBanBuon
            // 
            this.rdbBanBuon.AutoSize = true;
            this.rdbBanBuon.Location = new System.Drawing.Point(831, 29);
            this.rdbBanBuon.Name = "rdbBanBuon";
            this.rdbBanBuon.Size = new System.Drawing.Size(83, 20);
            this.rdbBanBuon.TabIndex = 2;
            this.rdbBanBuon.Text = "Bán buôn";
            this.rdbBanBuon.UseVisualStyleBackColor = true;
            // 
            // rdbBanLe
            // 
            this.rdbBanLe.AutoSize = true;
            this.rdbBanLe.Checked = true;
            this.rdbBanLe.Location = new System.Drawing.Point(742, 27);
            this.rdbBanLe.Name = "rdbBanLe";
            this.rdbBanLe.Size = new System.Drawing.Size(64, 20);
            this.rdbBanLe.TabIndex = 2;
            this.rdbBanLe.TabStop = true;
            this.rdbBanLe.Text = "Bán lẻ";
            this.rdbBanLe.UseVisualStyleBackColor = true;
            // 
            // lbKhachHang
            // 
            this.lbKhachHang.AutoSize = true;
            this.lbKhachHang.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbKhachHang.Location = new System.Drawing.Point(828, 68);
            this.lbKhachHang.Name = "lbKhachHang";
            this.lbKhachHang.Size = new System.Drawing.Size(79, 16);
            this.lbKhachHang.TabIndex = 0;
            this.lbKhachHang.Text = "Người tạo:";
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 1;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Controls.Add(this.groupBox1, 0, 1);
            this.tableLayoutPanel1.Controls.Add(this.panel1, 0, 3);
            this.tableLayoutPanel1.Controls.Add(this.toolStrip1, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.groupBox2, 0, 2);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 4;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 28F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 98F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 62F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(0, 0);
            this.tableLayoutPanel1.TabIndex = 5;
            // 
            // toolStrip1
            // 
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnSaveAndNew,
            this.btnPrintPreview,
            this.btnPrint,
            this.toolStripSeparator1,
            this.btnSearch});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(1, 25);
            this.toolStrip1.TabIndex = 6;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // btnSaveAndNew
            // 
            this.btnSaveAndNew.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.btnSaveAndNew.Image = global::QLBH.SaleProduct.Properties.Resources.Save_icon;
            this.btnSaveAndNew.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSaveAndNew.Name = "btnSaveAndNew";
            this.btnSaveAndNew.Size = new System.Drawing.Size(161, 22);
            this.btnSaveAndNew.Text = "Lưu và Thêm mới (Ctr+S)";
            this.btnSaveAndNew.Click += new System.EventHandler(this.btnSaveAndNew_Click);
            // 
            // btnPrintPreview
            // 
            this.btnPrintPreview.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.btnPrintPreview.Image = global::QLBH.SaleProduct.Properties.Resources.print_preview;
            this.btnPrintPreview.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnPrintPreview.Name = "btnPrintPreview";
            this.btnPrintPreview.Size = new System.Drawing.Size(114, 22);
            this.btnPrintPreview.Text = "Xem trước khi in";
            // 
            // btnPrint
            // 
            this.btnPrint.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.btnPrint.Image = global::QLBH.SaleProduct.Properties.Resources.printer_icon16;
            this.btnPrint.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.Size = new System.Drawing.Size(79, 22);
            this.btnPrint.Text = "In (Ctr+P)";
            this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // btnSearch
            // 
            this.btnSearch.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.btnSearch.Image = global::QLBH.SaleProduct.Properties.Resources.search_icon16;
            this.btnSearch.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSearch.Name = "btnSearch";
            this.btnSearch.Size = new System.Drawing.Size(118, 22);
            this.btnSearch.Text = "Tìm kiếm (Ctr+F)";
            this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(107, 50);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(66, 16);
            this.label2.TabIndex = 11;
            this.label2.Text = "Ngày tạo:";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(107, 23);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(76, 16);
            this.label4.TabIndex = 8;
            this.label4.Text = "Số HĐ (F1):";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.Location = new System.Drawing.Point(9, 91);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(102, 16);
            this.label3.TabIndex = 9;
            this.label3.Text = "Mã hàng (F2):";
            // 
            // rtbGhiChu
            // 
            this.rtbGhiChu.Location = new System.Drawing.Point(426, 37);
            this.rtbGhiChu.Name = "rtbGhiChu";
            this.rtbGhiChu.Size = new System.Drawing.Size(324, 80);
            this.rtbGhiChu.TabIndex = 13;
            this.rtbGhiChu.Text = "";
            // 
            // BanHangView
            // 
            this.BackColor = System.Drawing.SystemColors.Control;
            this.Controls.Add(this.tableLayoutPanel1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.rtbGhiChu);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Margin = new System.Windows.Forms.Padding(1);
            this.Name = "BanHangView";
            this.Size = new System.Drawing.Size(0, 0);
            this.Load += new System.EventHandler(this.BanHangView_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridViewEx1)).EndInit();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.tableLayoutPanel1.ResumeLayout(false);
            this.tableLayoutPanel1.PerformLayout();
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void cmbMahang_TextChanged(object sender, EventArgs e)
        {           
        }

        private void BuildDataSource()
        {
            
            
        }

        private void BanHangView_Load(object sender, EventArgs e)
        {
            InitDatatoControl();

            listViewPopup = new ListView();

            listViewPopup.View = View.Details;
            listViewPopup.Width = 500;


            listViewPopup.Height = 300;

            listViewPopup.Columns.Add("Mã MH", 150, HorizontalAlignment.Left);
            listViewPopup.Columns.Add("Tên MH", 350, HorizontalAlignment.Left);

            listViewPopup.MultiSelect = false;
            listViewPopup.GridLines = true;
            listViewPopup.FullRowSelect = true;

            // dataGridViewEx1.PopupControl = listViewPopup;

            //dataGridViewEx1.FilterContent += dataGridViewEx1_FilterContent;
            listViewPopup.SelectedIndexChanged += listViewPopup_SelectedIndexChanged;            

            tbxMatHangSeach.PopupControl = listViewPopup;
            tbxMatHangSeach.DataFilterEvent += tbxMatHangSeach_DataFilterEvent;

            tbxSoLuong.MouseClick += tbxSoLuong_MouseClick;  
          
           
            
        }

        void tbxSoLuong_MouseClick(object sender, MouseEventArgs e)
        {
            tbxSoLuong.SelectAll();
            tbxSoLuong.Focus();
        }

        void tbxMatHangSeach_DataFilterEvent(object sender, EventArgs e)
        {
            TextBox editTbx = (TextBox)sender;

            var items = mathangs.Where(mh => mh.MHTEN.Contains(editTbx.Text) || mh.MHCODE.Contains(editTbx.Text)).ToList();

            BuildListViewPopupItems(items);
        }
        private void BuildListViewPopupItems(List<MATHANG> mathangs)
        {
            listViewPopup.Items.Clear();
            foreach (MATHANG mh in mathangs)
            {
                ListViewItem item = new ListViewItem();
                item.Text = mh.MHCODE;
                item.SubItems.Add(mh.MHTEN);

                listViewPopup.Items.Add(item);
            }
        }

        void listViewPopup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewPopup.SelectedItems.Count > 0)
            {
                String mhID = listViewPopup.SelectedItems[0].SubItems[0].Text;
                MATHANG mh = MatHangController.GetInstance().FindBy(h => h.MHCODE == mhID).FirstOrDefault();

                if (mh != null)
                {
                    selected = mh;
                    tbxMatHangSeach.Text = selected.MHTEN;
                    tbxSoLuong.Text = "1";
                    tbxDonGia.Text = (rdbBanLe.Checked == true ? selected.GIABANLE : selected.GIABANBUON).ToString();
                }
                listViewPopup.Visible = false;
                tbxSoLuong.SelectAll();
                tbxSoLuong.Focus();                
            }        
        }

        private void InitDatatoControl()
        {
            if (model == null)
            {
                model = SaleProductModelFactory.GetHDBanHangModelInstance();
            }
            totalMoney = 0;
            lbsophieu.Text = model.SOHD;
            lbNgayTao.Text = model.NGAYTAO.ToShortDateString();
            rdbBanLe.Checked = model.BANLE;
            rdbBanBuon.Checked = !model.BANLE;

            lbNguoiTao.Text = model.USERNAME;

            lbKhachHang.Text = model.KHTEN;

            tbxTongTien.Text = totalMoney.ToString();


            List<KHO> khos = KhoController.GetInstance().GetAll().ToList();

//             khoHang = null;
             cmbKhoHang.DataSource = khos;
// 
             cmbKhoHang.DisplayMember = "TENKHO";

             KHO defaultKho = khos.Where(k => k.MACDINH == true).FirstOrDefault();
             if (defaultKho != null)
             {
                 cmbKhoHang.SelectedItem = defaultKho;
             }             
        }
        public override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode==Keys.F1)
            {
                tbxMatHangSeach.SelectAll();
                tbxMatHangSeach.Focus();
            }
            else if (e.KeyCode==Keys.F2)
            {
                tbxSoLuong.SelectAll();
                tbxSoLuong.Focus();
            }
            else if (e.KeyCode==Keys.F3)
            {
                tbxDonGia.SelectAll();
                tbxDonGia.Focus();
            }
            else if (e.KeyCode==Keys.F4)
            {
                tbxKhachHangTra.SelectAll();
                tbxKhachHangTra.Focus(); 
            }
            else if (e.KeyCode == Keys.F5)
            {
                tbxThue.SelectAll();
                tbxThue.Focus();
            }
            else if (e.KeyCode == Keys.F6)
            {
                tbxChietKhau.SelectAll();
                tbxChietKhau.Focus();
            }
            else if (e.KeyCode == Keys.F7)
            {
                cmbKhoHang.DroppedDown = true;
                cmbKhoHang.Focus();
            }
            else if (e.KeyCode == Keys.Enter)
            {
                btnAdd_Click(btnAdd, null);
            }
            else if (e.KeyCode == Keys.Delete)
            {
                if (btnDelete.Enabled)
                {
                    btnDelete.Focus();
                    btnDelete_Click(btnDelete, null);
                }
            }
            else if (e.Control && e.KeyCode == Keys.S)
            {
                btnSaveAndNew_Click(btnSaveAndNew, null);
            }
            else if (e.Control && e.KeyCode == Keys.F)
            {
                btnSearch_Click(btnSearch, null);
            }
            else if (e.Control && e.KeyCode == Keys.P)
            {
                btnPrint_Click(btnPrint, null);
            }                                  
        }

        private void cmbKhoHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            KHO khohang=cmbKhoHang.SelectedItem as KHO;

            List<Guid> mathangid=new List<Guid>();

            List<KHO_MATHANG> items= Kho_MathangController.GetInstance().FindBy(k => k.KHOID==khohang.KHOID).ToList();

            foreach (KHO_MATHANG kh in items)
            {
                mathangid.Add(kh.MHID);
            }
            mathangs = MatHangController.GetInstance().FindBy(mh => mathangid.Contains(mh.MHID)).ToList();
        }

        private void linklbKhachHang_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //select customer from exist list or add new
            FrmSelectCustomer selectCustomerDlg = new FrmSelectCustomer();
            if (selectCustomerDlg.ShowDialog()==DialogResult.OK)
            {
                model.KHACHHANGID = selectCustomerDlg.SelectedKH.KHID;
                model.KHTEN = selectCustomerDlg.SelectedKH.TENKH;

                lbKhachHang.Text = model.KHTEN;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DataInputHelper.CheckIntValue(tbxSoLuong.Text, "Số lượng");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxSoLuong.SelectAll();
                tbxSoLuong.Focus();
                return;
            }
            try
            {
                DataInputHelper.CheckDecimalData(tbxDonGia.Text, "Đơn giá");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxDonGia.SelectAll();
                tbxDonGia.Focus();
                return;
            }
            if (functionType == eDlgType.ADD)
            {
                if (selected != null)
                {
                    IHD_MatHangModel item = model.MatHang.Where(mh => mh.MaMH == selected.MHCODE).FirstOrDefault();
                    if (item==null)
                    {
                        item = SaleProductModelFactory.GetHDMatHangModelInstance();
                        item.InitFromMatHangModel(selected, rdbBanLe.Checked);

                        model.MatHang.Add(item);
                    }   
                    
                    int soluong = int.Parse(tbxSoLuong.Text);
                    item.SOLUONG += soluong;
                    item.KHOID = (cmbKhoHang.SelectedItem as KHO).KHOID;

                    decimal dongia = decimal.Parse(tbxDonGia.Text);
                    item.GIABAN = dongia;

                    totalMoney = 0;

                    foreach (var mh in model.MatHang)
                    {
                        totalMoney += mh.THANHTIEN;
                    }

                    tbxTongTien.Text = totalMoney.ToString();

                    UpdateDataToGrid();
                    
                    tbxMatHangSeach.Focus();
                }
            }
            else
            {
                //Edit
                int soluong = int.Parse(tbxSoLuong.Text);
                editItem.SOLUONG = soluong;

                decimal dongia = decimal.Parse(tbxDonGia.Text);
                editItem.GIABAN = dongia;

                UpdateDataToGrid();

                tbxMatHangSeach.Enabled = true;
                tbxMatHangSeach.Focus();

                btnAdd.Text = "Thêm";
                btnAdd.Image = global::QLBH.SaleProduct.Properties.Resources.add_1_icon;
                functionType = eDlgType.ADD;

                btnDelete.Enabled = true;
            }
        }

        private void UpdateDataToGrid()
        {
            dataGridViewEx1.DataSource = new List<IHD_MatHangModel>(model.MatHang);

            totalMoney = 0;
            foreach (IHD_MatHangModel mh in model.MatHang)
            {
                totalMoney += mh.THANHTIEN;
            }
            tbxTongTien.Text = totalMoney.ToString();
        }      

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count>0)
            {
                var item = dataGridViewEx1.SelectedRows[0].DataBoundItem as IHD_MatHangModel;                
                model.MatHang.Remove(item);
                dataGridViewEx1.DataSource = new List<IHD_MatHangModel>(model.MatHang);

                UpdateDataToGrid();            
            }                                                                                                       
        }

        private void dataGridViewEx1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count>0)
            {
                btnDelete.Enabled = true;
            }
            else
            {
                btnDelete.Enabled = false;
            }
        }

        private void dataGridViewEx1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count > 0)
            {
                IHD_MatHangModel itemSelected = dataGridViewEx1.SelectedRows[0].DataBoundItem as IHD_MatHangModel;

                if (itemSelected != null)
                {
                    editItem = itemSelected;
                    tbxMatHangSeach.SetText(editItem.TenMH);
                    tbxSoLuong.Text = editItem.SOLUONG.ToString();

                    btnAdd.Text = "Sửa";
                    functionType = eDlgType.EDIT;
                    this.btnAdd.Image = global::QLBH.SaleProduct.Properties.Resources.edit_icon;

                    tbxSoLuong.SelectAll();
                    tbxSoLuong.Focus();

                    tbxMatHangSeach.Enabled = false;

                    btnDelete.Enabled = false;
                }
            }
        }

        private void tbxTongTien_TextChanged(object sender, EventArgs e)
        {
            CalcTongTien();
        }
        private void CalcTongTien()
        {
            thue = double.Parse(tbxThue.Text);
            chietkhau = decimal.Parse(tbxChietKhau.Text);
            model.TONGTIEN = totalMoney * (decimal)(1 + thue / 100) - chietkhau;

            tbxTongTienTT.Text = model.TONGTIEN.ToString();
        }

        private void tbxThue_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataInputHelper.CheckDecimalData(tbxThue.Text, "Thuế");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxThue.SelectAll();
                tbxThue.Focus();
                return;
            }
            if (decimal.Parse(tbxThue.Text) < 0)
            {
                ShowMessage.Instance.ShowError("Thuế phải là số dương"); ;
                tbxThue.SelectAll();
                tbxThue.Focus();
                return;
            }

            CalcTongTien();
        }

        private void tbxChietKhau_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataInputHelper.CheckDecimalData(tbxChietKhau.Text, "Chiết khấu");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxChietKhau.SelectAll();
                tbxChietKhau.Focus();
                return;
            }
            if (decimal.Parse(tbxChietKhau.Text) < 0)
            {
                ShowMessage.Instance.ShowError("chiết khấu phải là số dương"); ;
                tbxChietKhau.SelectAll();
                tbxChietKhau.Focus();
                return;
            }

            CalcTongTien();
        }

        private void tbxKhachHangTra_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataInputHelper.CheckDecimalData(tbxKhachHangTra.Text, "Khách hàng trả");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxKhachHangTra.SelectAll();
                tbxKhachHangTra.Focus();
                return;
            }
            if (decimal.Parse(tbxKhachHangTra.Text) < 0)
            {
                ShowMessage.Instance.ShowError("Tiền khách hàng trả phải là số dương"); ;
                tbxKhachHangTra.SelectAll();
                tbxKhachHangTra.Focus();
                return;
            }

            khachangtra = decimal.Parse(tbxKhachHangTra.Text);
            tbxTienThua.Text = (khachangtra - model.TONGTIEN).ToString();
        }

        private void tbxTongTienTT_TextChanged(object sender, EventArgs e)
        {
            khachangtra = decimal.Parse(tbxKhachHangTra.Text);
            tbxTienThua.Text = (khachangtra - model.TONGTIEN).ToString();
        }

        private void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (model.MatHang.Count==0)
                {
                    ShowMessage.Instance.ShowError("Bạn chưa chọn mặt hàng nào");
                    return;
                }
                Cursor = Cursors.WaitCursor;
                if (dlgType == eDlgType.ADD)
                {
                    // add new
                    
                    if (CheckMHAvailable())
                    {
                        HOADONBANHANG hdbh = model.ToEntity();
                        HDBanHangController.GetInstance().Add(hdbh);

                        //create new HD
                        NewHD();
                    }                               
                }
                else
                {
                    //edit
                    HOADONBANHANG hdbh = model.ToEntity();
                    HDBanHangController.GetInstance().Edit(hdbh);

                    //create new HD
                    NewHD();

                }
                Cursor = Cursors.Default;
            }
            catch (System.Exception ex)
            { 
                Cursor = Cursors.Default;
                ShowMessage.Instance.ShowError(ex.Message);
            }
        }
        private bool CheckMHAvailable()
        {
            foreach (IHD_MatHangModel mh in model.MatHang)
            {
                KHO_MATHANG kh_mh = Kho_MathangController.GetInstance().FindBy(kh => 
                    kh.MHID == mh.MHID).FirstOrDefault();
                if (kh_mh != null)
                {
                   if (kh_mh.SOLUONG<mh.SOLUONG)
                   {
                       ShowMessage.Instance.ShowError("Mặt hàng: '" + mh.TenMH + "' có lượng tồn kho nhỏ hơn " + mh.SOLUONG.ToString());
                       return false;
                   }
                }
                else
                {
                    ShowMessage.Instance.ShowError("Mặt hàng: '" + mh.TenMH + "' không tồn tại trong kho hàng!");
                    return false;
                }                 
            }
            return true;
        }
        private void NewHD()
        {
            dlgType = eDlgType.ADD;
            model = null;
            model = SaleProductModelFactory.GetHDBanHangModelInstance();

            totalMoney = 0;
            lbsophieu.Text = model.SOHD;
            lbNgayTao.Text = model.NGAYTAO.ToShortDateString();
            rdbBanLe.Checked = model.BANLE;
            rdbBanBuon.Checked = !model.BANLE;

            lbNguoiTao.Text = model.USERNAME;

            lbKhachHang.Text = model.KHTEN;

            tbxTongTien.Text = totalMoney.ToString();

            UpdateDataToGrid();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchHDBanHangDlg searchDlg = new SearchHDBanHangDlg();
            if (searchDlg.ShowDialog()==DialogResult.OK)
            {
                IHDBanHang pnkmodel = searchDlg.Model;
                model.InitFromEntity(pnkmodel.ToEntity());

                dlgType = eDlgType.EDIT;

                InitDatatoControl();

                UpdateDataToGrid();
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }

        private void tbxDonGia_MouseClick(object sender, MouseEventArgs e)
        {
            tbxDonGia.SelectAll();
            tbxDonGia.Focus();
        }
                
    }
}
